<template>
  <el-dialog :visible.sync="dialogVisible" title="药品选择" width="1000px">
    <div class="container">
      <!-- 表格 -->
      <el-table
        v-loading="tableLoading"
        :data="tableData"
        :stripe="true"
        :border="true"
        height="100%"
        style="width: 100%"
      >
        <el-table-column type="index" width="70" align="center" label="序号" />
        <el-table-column prop="field01" align="center" label="ID" show-overflow-tooltip></el-table-column>
        <el-table-column prop="field02" align="center" label="法规名称" show-overflow-tooltip></el-table-column>
        <el-table-column prop="field03" align="center" label="审核状态" show-overflow-tooltip></el-table-column>
        <el-table-column prop="field04" align="center" label="关联关系" show-overflow-tooltip></el-table-column>
        <el-table-column prop="field05" align="center" label="说明" show-overflow-tooltip></el-table-column>
      </el-table>
    </div>
    <el-pagination
      :current-page="pagination.current"
      :page-sizes="pagination.sizes"
      :page-size="pagination.size"
      :total="pagination.total"
      background
      layout="total, sizes, prev, pager, next, jumper"
      @size-change="sizeChange"
      @current-change="pageChange"
    >
    </el-pagination>
  </el-dialog>
</template>
<script>
import { remove, getData } from '@/mock/index'

// 搜索分页表
import tablePaginationMixin from '@/mixins/tablePagination.js'
export default {
  name: 'RelationDialog',
  mixins: [tablePaginationMixin],
  props: {
    // 是否显示
    visible: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      loading: false
    }
  },
  computed: {
    dialogVisible: {
      get: function () {
        return this.visible
      },
      set: function (newValue) {
        this.$emit('update:visible', newValue)
      }
    }
  },
  created () {
    this.getTableData()
  },
  methods: {
    // 获取列表
    async getTableData () {
      // 模拟表格数据
      const tableData = {
        current: 1,
        pages: 1,
        records: [
          {
            field01: '项目A',
            field02: '',
            field03: '2019-3-12',
            field04: '论证阶段',
            field05: '210000',
            field06: '',
            uuid: 'uuid'
          },
          {
            field01: '项目A',
            field02: '',
            field03: '2019-3-12',
            field04: '论证阶段',
            field05: '210000',
            field06: '',
            uuid: 'uuid'
          },
        ],
        size: 20,
        total: 1
      }
      const { current, size } = this.pagination
      this.tableLoading = true
      const res = await getData({ current, size }, tableData)
      this.tableLoading = false
      if (res.code === '0000') {
        const { current, size, total, records } = res.data
        // console.log(res)
        this.pagination = Object.assign({}, this.pagination, { current, size, total })
        this.tableData = records
      }
    },
    // 删除
    delRow (uuid) {
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const res = await remove({ userUuid: uuid })
        if (res.code === '0000') {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          // 删除最后一个时处理
          if (this.tableData.length === 1 && this.pagination.current > 1) {
            this.pagination.current = this.pagination.current - 1
          }
          // 更新表格
          this.getTableData()
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="scss" scoped>
.container {
  padding-bottom: 20px;
  height: 300px;
}
</style>
